####social influence paper####

library(Zelig)
library(foreign)
library(MatchIt)
library(car)
library(RItools)
library(ggplot2)
library(reshape2)

###load data###

nsf3 <-read.csv ("~/Documents/RFiles/nsf3.csv")
subjects<-read.csv ("~/Documents/RFiles/subedit.csv")

###first, I want only the data from the primary###


primary<-subset(nsf3, stage_id == 904)


###now, just want data on the stimulus items###

primary1 <-subset(primary, item_type_name == "stim")

###working file needs: subject id, event type, groups, item associations###
head(primary1)

working1<-subset(primary1, select = c("event_subject_id", "event_type","group_name","item_name","item_id", "item_alt_id", "item_altassoc_name"))

###next, I need to reshape the data so that item names are the identifier###

trial <-melt(working1, id = c("event_subject_id", "item_name","group_name","item_altassoc_name", "item_id","item_alt_id"))

###I want item name to be the rows, rest to be the columns####

trial1 <- dcast(trial, ...~value)
colnames(trial1)

####getting rid of the 'before items'(these are items that appear before the actual primary stage)####

trial2<-subset(trial1, item_alt_id != 0)

###removing some redundant columns###

trial3<-trial2[c(-8,-9)]


###adding in the column names###

colnames(trial3)<-c("SubID", "ItemName","Group","Candidate","ItemID","ItemAltID", "Variable", "ItemAppear","ItemDisappear","ItemOpened","ItemClosed","Share","Like","Dislike","CommentOpen","CommentClose","CommentLeft")

####I need to add dummy variables to show which candidate each item referred to, as well dummy variables indicating whether the item was about the preferred candidate####
###candidate dummy###
yes <-1
no <-0

trial3$C1 <-ifelse(trial3$Candidate == 'CandidateOne', yes, no)
trial3$C2 <-ifelse(trial3$Candidate == 'CandidateTwo', yes, no)
trial3$C3 <-ifelse(trial3$Candidate == 'CandidateThree', yes, no)




####getting vote preferences####
####merging vote variable from the subject file###

VoteChoice<-subset(subjects, select = c("subject_id","FinalVote.FINALVOTE"))
colnames(VoteChoice)<-c("SubID","VoteChoice")


trial.3<-merge(trial3, VoteChoice, by = "SubID")



####creating preference dummy to indicate whether an info item was about the candidate subject voted for####

x1<-rep.int(999, 30149)
trial.3$Preferred <-x1

trial.3$Preferred[trial.3$Candidate == "CandidateOne" & trial.3$VoteChoice == 1] <- 1
trial.3$Preferred[trial.3$Candidate == "CandidateTwo" & trial.3$VoteChoice == 2] <- 1
trial.3$Preferred[trial.3$Candidate == "CandidateThree" & trial.3$VoteChoice == 3] <- 1

trial.3$Preferred<-recode(trial.3$Preferred, "999 = 0")

summary(trial.3$Preferred)

####creating experimental group dummies. Combining Republican and Democratic versions###
###NOTE: The group names indicate manipulated Canidate (1,2,3), whether Comments were allowed, whether the social cues were positive (Like condition) or negative (Dislike) and whether it was the Democratic or Republican version###
####so, group C3CDD means info about Candidate 3 was manipulated, Comments were available, Dislikes were more common, and it was the Democratic version####
x1<-rep.int(0, 30149)
trial.3$Control <-x1

trial.3$Control[trial.3$Group == "DemPrimaryControl" | trial.3$Group == "RepPrimaryControl"] <- 1

x1<-rep.int(0, 30149)
trial.3$CommentOnly <-x1

trial.3$CommentOnly[trial.3$Group == "C2CD" | trial.3$Group == "C2CR"] <- 1

x1<-rep.int(0, 30149)
trial.3$LikesOnly <-x1

trial.3$LikesOnly[trial.3$Group == "C1LR" | trial.3$Group == "C3LD"] <- 1

x1<-rep.int(0, 30149)
trial.3$DislikesOnly <-x1

trial.3$DislikesOnly[trial.3$Group == "C1DR" | trial.3$Group == "C3DD"] <- 1

x1<-rep.int(0, 30149)
trial.3$LikesCom <-x1

trial.3$LikesCom[trial.3$Group == "C1CLR" | trial.3$Group == "C3CLD"] <- 1


x1<-rep.int(0, 30149)
trial.3$DislikesCom <-x1

trial.3$DislikesCom[trial.3$Group == "C1CDR" | trial.3$Group == "C3CDD"] <- 1

####republican and democratic primary dummies [note: not used in the analysis] ###

x1<-rep.int(0, 30149)
trial.3$DemPrimary <-x1

trial.3$DemPrimary[trial.3$Group == "DemPrimaryControl" | trial.3$Group == "C3LD" | trial.3$Group == "C3DD" | trial.3$Group == "C3CLD"| trial.3$Group == "C3CDD" | trial.3$Group == "C2CD"] <- 1

x1<-rep.int(0, 30149)
trial.3$RepPrimary <-x1

trial.3$RepPrimary[trial.3$DemPrimary == 0] <- 1

summary(trial.3)


###creating a dummy variable to indicate whether the item referred to the manipulated candidate###

x1<-rep.int(0, 30149)
trial.3$Manipulated <-x1

trial.3$Manipulated[trial.3$Group == "RepPrimaryControl" & trial.3$Candidate == "CandidateOne"] <- 1
trial.3$Manipulated[trial.3$Group == "DemPrimaryControl" & trial.3$Candidate == "CandidateThree"] <- 1

trial.3$Manipulated[trial.3$Group == "C2CR" & trial.3$Candidate == "CandidateOne"] <- 1
trial.3$Manipulated[trial.3$Group == "C2CD" & trial.3$Candidate == "CandidateThree"] <- 1

trial.3$Manipulated[trial.3$Group == "C1LR" & trial.3$Candidate == "CandidateOne"] <- 1
trial.3$Manipulated[trial.3$Group == "C3LD" & trial.3$Candidate == "CandidateThree"] <- 1

trial.3$Manipulated[trial.3$Group == "C1DR" & trial.3$Candidate == "CandidateOne"] <- 1
trial.3$Manipulated[trial.3$Group == "C3DD" & trial.3$Candidate == "CandidateThree"] <- 1

trial.3$Manipulated[trial.3$Group == "C1CLR" & trial.3$Candidate == "CandidateOne"] <- 1
trial.3$Manipulated[trial.3$Group == "C3CLD" & trial.3$Candidate == "CandidateThree"] <- 1

trial.3$Manipulated[trial.3$Group == "C1CDR" & trial.3$Candidate == "CandidateOne"] <- 1
trial.3$Manipulated[trial.3$Group == "C3CDD" & trial.3$Candidate == "CandidateThree"] <- 1



####last, want to collapse some groups based on the valance of the cues####

trial.3$Control2 <-x1
trial.3$Control2[trial.3$Group == "RepPrimaryControl" | trial.3$Group == "DemPrimaryControl" | trial.3$Group == "C2CR" | trial.3$Group == "C2CD"] <- 1

trial.3$PositiveGroup <- x1
trial.3$PositiveGroup[trial.3$Group == "C1LR" | trial.3$Group == "C3LD" | trial.3$Group == "C1CLR" | trial.3$Group == "C3CLD"] <- 1

trial.3$NegativeGroup <- x1
trial.3$NegativeGroup[trial.3$Group == "C1DR" | trial.3$Group == "C3DD" | trial.3$Group == "C1CDR" | trial.3$Group == "C3CDD"] <- 1

####finally, control group vs. some manipulation####
x2<-rep.int(1, 30149)
trial.3$SomeCues <-x2
trial.3$SomeCues[trial.3$Group == "RepPrimaryControl" | trial.3$Group == "DemPrimaryControl"] <- 0




###next, reorganize the stimuli by topic: D = demographic info; P = policy info; G = general info####

trial.3$Topic<-recode(trial.3$ItemName, "'R1D1' = 'D1'; 'R2D1' = 'D1';'R3D1' = 'D1';'R1D2' = 'D2'; 'R2D2' = 'D2';'R3D2' = 'D2';
'R1D3' = 'D3'; 'R2D3' = 'D3';'R3D3' = 'D3';'R1D4' = 'D4'; 'R2D4' = 'D4';'R3D4' = 'D4';'R1D5' = 'D5'; 'R2D5' = 'D5';'R3D5' = 'D5';
'R1D6' = 'D6'; 'R2D6' = 'D6';'R3D6' = 'D6';'R1D7' = 'D7'; 'R2D7' = 'D7';'R3D7' = 'D7';'R1D8' = 'D8'; 'R2D8' = 'D8';'R3D8' = 'D8';
                        'R1D9' = 'D9'; 'R2D9' = 'D9';'R3D9' = 'D9';'R1D10' = 'D10'; 'R2D10' = 'D10';'R3D10' = 'D10';
                        'R1G1' = 'G1'; 'R2G1' = 'G1'; 'R3G1' = 'G1'; 'R1G2' = 'G2'; 'R2G2' = 'G2'; 'R3G2' = 'G2';'R1G3' = 'G3'; 'R2G3' = 'G3'; 'R3G3' = 'G3';
                        'R1G4' = 'G4'; 'R2G4' = 'G4'; 'R3G4' = 'G4';'R1G5' = 'G5'; 'R2G5' = 'G5'; 'R3G5' = 'G5';'R1G6' = 'G6'; 'R2G6' = 'G6'; 'R3G6' = 'G6';
                        'R1G7' = 'G7'; 'R2G7' = 'G7'; 'R3G7' = 'G7';'R1G8' = 'G8'; 'R2G8' = 'G8'; 'R3G8' = 'G8';
                        'R1P1' = 'P1'; 'R2P1' = 'P1'; 'R3P1' = 'P1';'R3P1d' = 'P1'; 'R1P2' = 'P2'; 'R2P2' = 'P2'; 'R3P2' = 'P2';'R1P3' = 'P3'; 'R2P3' = 'P3'; 'R3P3' = 'P3';
                        'R1P4' = 'P4'; 'R2P4' = 'P4'; 'R3P4' = 'P4';'R1P4d' = 'P4'; 'R1P5' = 'P5'; 'R2P5' = 'P5'; 'R3P5' = 'P5';'R1P5d' = 'P5'; 'R3P5d' = 'P5';
                        'R1P6' = 'P6'; 'R2P6' = 'P6'; 'R3P6' = 'P6';'R3P6d' = 'P6'; 'R1P7' = 'P7'; 'R2P7' = 'P7'; 'R3P7' = 'P7';
                        'R1P8' = 'P8'; 'R2P8' = 'P8'; 'R3P8' = 'P8';'R1P9' = 'P9'; 'R2P9' = 'P9'; 'R3P9' = 'P9';'R1P9d' = 'P9';'R2P9d' = 'P9'; 'R3P9d' = 'P9';
                        'R1P10' = 'P10';'R1P10d' = 'P10'; 'R2P10' = 'P10'; 'R3P10' = 'P10';'R1D10d' = 'P10'; 'R1P11' = 'P11'; 'R2P11' = 'P11'; 'R3P11' = 'P11';'R3P11d' = 'P11';
                        'R1P12' = 'P12'; 'R2P12' = 'P12'; 'R3P12' = 'P12';'R1P13' = 'P13'; 'R2P13' = 'P13'; 'R3P13' = 'P13';
                        'R1P14' = 'P14'; 'R2P14' = 'P14'; 'R3P14' = 'P14';'R1P15' = 'P15'; 'R2P15' = 'P15'; 'R3P15' = 'P15';'R3P15d'='P15'")


###group by policy/non-policy. policy items set to 1###

trial.3$ItemType<-recode(trial.3$Topic, "c('D1','D2','D3','D4','D5','D6','D7','D8','D9','D10','G1','G2','G3'
,'G4','G5','G6','G7') = 0; else = 1")


###now, want to group the stimuli by the level of starting cues {see description in the paper. "high" cue items have between 20 - 30 signals, mid-cues 10 - 20###

###hi activity items###

x1<-rep.int(0, 30149)
trial.3$HiCues <-x1

trial.3$HiCues[trial.3$Group == "C1DR" & (trial.3$ItemName == "R1D4" | trial.3$ItemName == "R1D6" | trial.3$ItemName == "R1D9" |
                trial.3$ItemName == "R1G8" | trial.3$ItemName == "R1P1" | trial.3$ItemName == "R1P2" | trial.3$ItemName == "R1P5" |
                trial.3$ItemName == "R1P6" | trial.3$ItemName == "R1P7" | trial.3$ItemName == "R1P8" | trial.3$ItemName == "R1P15")] <- 1

trial.3$HiCues[trial.3$Group == "C1LR" & (trial.3$ItemName == "R1D4" | trial.3$ItemName == "R1D6" | trial.3$ItemName == "R1D9" |
                                          trial.3$ItemName == "R1G8" | trial.3$ItemName == "R1P1" | trial.3$ItemName == "R1P2" | trial.3$ItemName == "R1P5" |
                                          trial.3$ItemName == "R1P6" | trial.3$ItemName == "R1P7" | trial.3$ItemName == "R1P8" | trial.3$ItemName == "R1P15")] <- 1


trial.3$HiCues[trial.3$Group == "C1CLR" & (trial.3$ItemName == "R1D4" | trial.3$ItemName == "R1D6" | trial.3$ItemName == "R1D9" |
                                          trial.3$ItemName == "R1G8" | trial.3$ItemName == "R1P1" | trial.3$ItemName == "R1P2" | trial.3$ItemName == "R1P5" |
                                          trial.3$ItemName == "R1P6" | trial.3$ItemName == "R1P7" | trial.3$ItemName == "R1P8" | trial.3$ItemName == "R1P15")] <- 1


trial.3$HiCues[trial.3$Group == "C1CDR" & (trial.3$ItemName == "R1D4" | trial.3$ItemName == "R1D6" | trial.3$ItemName == "R1D9" |
                                          trial.3$ItemName == "R1G8" | trial.3$ItemName == "R1P1" | trial.3$ItemName == "R1P2" | trial.3$ItemName == "R1P5" |
                                          trial.3$ItemName == "R1P6" | trial.3$ItemName == "R1P7" | trial.3$ItemName == "R1P8" | trial.3$ItemName == "R1P15")] <- 1

trial.3$HiCues[trial.3$Group == "C2CR" & (trial.3$ItemName == "R1D4" | trial.3$ItemName == "R1D6" | trial.3$ItemName == "R1D9" |
                                          trial.3$ItemName == "R1G8" | trial.3$ItemName == "R1P1" | trial.3$ItemName == "R1P2" | trial.3$ItemName == "R1P5" |
                                          trial.3$ItemName == "R1P6" | trial.3$ItemName == "R1P7" | trial.3$ItemName == "R1P8" | trial.3$ItemName == "R1P15")] <- 1

trial.3$HiCues[trial.3$Group == "RepPrimaryControl" & (trial.3$ItemName == "R1D4" | trial.3$ItemName == "R1D6" | trial.3$ItemName == "R1D9" |
                                          trial.3$ItemName == "R1G8" | trial.3$ItemName == "R1P1" | trial.3$ItemName == "R1P2" | trial.3$ItemName == "R1P5" |
                                          trial.3$ItemName == "R1P6" | trial.3$ItemName == "R1P7" | trial.3$ItemName == "R1P8" | trial.3$ItemName == "R1P15")] <- 1


trial.3$HiCues[trial.3$Group == "C3DD" & (trial.3$ItemName == "R3D4" | trial.3$ItemName == "R3D6" | trial.3$ItemName == "R3D9" |
                                          trial.3$ItemName == "R3G8" | trial.3$ItemName == "R3P1" | trial.3$ItemName == "R3P2" | trial.3$ItemName == "R3P5" |
                                          trial.3$ItemName == "R3P6" | trial.3$ItemName == "R3P7" | trial.3$ItemName == "R3P8" | trial.3$ItemName == "R3P15")] <- 1

trial.3$HiCues[trial.3$Group == "C3LD" & (trial.3$ItemName == "R3D4" | trial.3$ItemName == "R3D6" | trial.3$ItemName == "R3D9" |
                                         trial.3$ItemName == "R3G8" | trial.3$ItemName == "R3P1" | trial.3$ItemName == "R3P2" | trial.3$ItemName == "R3P5" |
                                          trial.3$ItemName == "R3P6" | trial.3$ItemName == "R3P7" | trial.3$ItemName == "R3P8" | trial.3$ItemName == "R3P15")] <- 1


trial.3$HiCues[trial.3$Group == "C3CDD" & (trial.3$ItemName == "R3D4" | trial.3$ItemName == "R3D6" | trial.3$ItemName == "R3D9" |
                                          trial.3$ItemName == "R3G8" | trial.3$ItemName == "R3P1" | trial.3$ItemName == "R3P2" | trial.3$ItemName == "R3P5" |
                                          trial.3$ItemName == "R3P6" | trial.3$ItemName == "R3P7" | trial.3$ItemName == "R3P8" | trial.3$ItemName == "R3P15")] <- 1


trial.3$HiCues[trial.3$Group == "C3CLD" & (trial.3$ItemName == "R3D4" | trial.3$ItemName == "R3D6" | trial.3$ItemName == "R3D9" |
                                          trial.3$ItemName == "R3G8" | trial.3$ItemName == "R3P1" | trial.3$ItemName == "R3P2" | trial.3$ItemName == "R3P5" |
                                          trial.3$ItemName == "R3P6" | trial.3$ItemName == "R3P7" | trial.3$ItemName == "R3P8" | trial.3$ItemName == "R3P15")] <- 1

trial.3$HiCues[trial.3$Group == "C2CD" & (trial.3$ItemName == "R3D4" | trial.3$ItemName == "R3D6" | trial.3$ItemName == "R3D9" |
                                          trial.3$ItemName == "R3G8" | trial.3$ItemName == "R3P1" | trial.3$ItemName == "R3P2" | trial.3$ItemName == "R3P5" |
                                          trial.3$ItemName == "R3P6" | trial.3$ItemName == "R3P7" | trial.3$ItemName == "R3P8" | trial.3$ItemName == "R3P15")] <- 1

trial.3$HiCues[trial.3$Group == "DemPrimaryControl" & (trial.3$ItemName == "R3D4" | trial.3$ItemName == "R3D6" | trial.3$ItemName == "R3D9" |
                                          trial.3$ItemName == "R3G8" | trial.3$ItemName == "R3P1" | trial.3$ItemName == "R3P2" | trial.3$ItemName == "R3P5" |
                                          trial.3$ItemName == "R3P6" | trial.3$ItemName == "R3P7" | trial.3$ItemName == "R3P8" | trial.3$ItemName == "R3P15")] <- 1




###mid activity items###

x1<-rep.int(0, 30149)
trial.3$MidCues <-x1

trial.3$MidCues[trial.3$Group == "C1DR" & (trial.3$ItemName == "R1D10" | trial.3$ItemName == "R1D3" | trial.3$ItemName == "R1D5" |
                                          trial.3$ItemName == "R1D8" | trial.3$ItemName == "R1G1" | trial.3$ItemName == "R1G2" | trial.3$ItemName == "R1G5" |
                                          trial.3$ItemName == "R1G7" | trial.3$ItemName == "R1P10" | trial.3$ItemName == "R1P13" | trial.3$ItemName == "R1P14")] <- 1

trial.3$MidCues[trial.3$Group == "C1LR" & (trial.3$ItemName == "R1D10" | trial.3$ItemName == "R1D3" | trial.3$ItemName == "R1D5" |
                                           trial.3$ItemName == "R1D8" | trial.3$ItemName == "R1G1" | trial.3$ItemName == "R1G2" | trial.3$ItemName == "R1G5" |
                                           trial.3$ItemName == "R1G7" | trial.3$ItemName == "R1P10" | trial.3$ItemName == "R1P13" | trial.3$ItemName == "R1P14")] <- 1

trial.3$MidCues[trial.3$Group == "C1CLR" & (trial.3$ItemName == "R1D10" | trial.3$ItemName == "R1D3" | trial.3$ItemName == "R1D5" |
                                           trial.3$ItemName == "R1D8" | trial.3$ItemName == "R1G1" | trial.3$ItemName == "R1G2" | trial.3$ItemName == "R1G5" |
                                           trial.3$ItemName == "R1G7" | trial.3$ItemName == "R1P10" | trial.3$ItemName == "R1P13" | trial.3$ItemName == "R1P14")] <- 1

trial.3$MidCues[trial.3$Group == "C1CDR" & (trial.3$ItemName == "R1D10" | trial.3$ItemName == "R1D3" | trial.3$ItemName == "R1D5" |
                                           trial.3$ItemName == "R1D8" | trial.3$ItemName == "R1G1" | trial.3$ItemName == "R1G2" | trial.3$ItemName == "R1G5" |
                                           trial.3$ItemName == "R1G7" | trial.3$ItemName == "R1P10" | trial.3$ItemName == "R1P13" | trial.3$ItemName == "R1P14")] <- 1

trial.3$MidCues[trial.3$Group == "C2CR" & (trial.3$ItemName == "R1D10" | trial.3$ItemName == "R1D3" | trial.3$ItemName == "R1D5" |
                                           trial.3$ItemName == "R1D8" | trial.3$ItemName == "R1G1" | trial.3$ItemName == "R1G2" | trial.3$ItemName == "R1G5" |
                                           trial.3$ItemName == "R1G7" | trial.3$ItemName == "R1P10" | trial.3$ItemName == "R1P13" | trial.3$ItemName == "R1P14")] <- 1

trial.3$MidCues[trial.3$Group == "RepPrimaryControl" & (trial.3$ItemName == "R1D10" | trial.3$ItemName == "R1D3" | trial.3$ItemName == "R1D5" |
                                           trial.3$ItemName == "R1D8" | trial.3$ItemName == "R1G1" | trial.3$ItemName == "R1G2" | trial.3$ItemName == "R1G5" |
                                           trial.3$ItemName == "R1G7" | trial.3$ItemName == "R1P10" | trial.3$ItemName == "R1P13" | trial.3$ItemName == "R1P14")] <- 1


trial.3$MidCues[trial.3$Group == "C3DD" & (trial.3$ItemName == "R3D10" | trial.3$ItemName == "R3D3" | trial.3$ItemName == "R3D5" |
                                           trial.3$ItemName == "R3D8" | trial.3$ItemName == "R3G1" | trial.3$ItemName == "R3G2" | trial.3$ItemName == "R3G5" |
                                           trial.3$ItemName == "R3G7" | trial.3$ItemName == "R3P10" | trial.3$ItemName == "R3P13" | trial.3$ItemName == "R3P14")] <- 1

trial.3$MidCues[trial3$Group == "C3LD" & (trial.3$ItemName == "R3D10" | trial.3$ItemName == "R3D3" | trial.3$ItemName == "R3D5" |
                                           trial.3$ItemName == "R3D8" | trial.3$ItemName == "R3G1" | trial.3$ItemName == "R3G2" | trial.3$ItemName == "R3G5" |
                                           trial.3$ItemName == "R3G7" | trial.3$ItemName == "R3P10" | trial.3$ItemName == "R3P13" | trial.3$ItemName == "R3P14")] <- 1


trial.3$MidCues[trial.3$Group == "C3CDD" & (trial.3$ItemName == "R3D10" | trial.3$ItemName == "R3D3" | trial.3$ItemName == "R3D5" |
                                           trial.3$ItemName == "R3D8" | trial.3$ItemName == "R3G1" | trial.3$ItemName == "R3G2" | trial.3$ItemName == "R3G5" |
                                           trial.3$ItemName == "R3G7" | trial.3$ItemName == "R3P10" | trial.3$ItemName == "R3P13" | trial.3$ItemName == "R3P14")] <- 1

trial.3$MidCues[trial.3$Group == "C3CLD" & (trial.3$ItemName == "R3D10" | trial.3$ItemName == "R3D3" | trial.3$ItemName == "R3D5" |
                                           trial.3$ItemName == "R3D8" | trial.3$ItemName == "R3G1" | trial.3$ItemName == "R3G2" | trial.3$ItemName == "R3G5" |
                                           trial.3$ItemName == "R3G7" | trial.3$ItemName == "R3P10" | trial.3$ItemName == "R3P13" | trial.3$ItemName == "R3P14")] <- 1


trial.3$MidCues[trial.3$Group == "C2CD" & (trial.3$ItemName == "R3D10" | trial.3$ItemName == "R3D3" | trial.3$ItemName == "R3D5" |
                                           trial.3$ItemName == "R3D8" | trial.3$ItemName == "R3G1" | trial.3$ItemName == "R3G2" | trial.3$ItemName == "R3G5" |
                                           trial.3$ItemName == "R3G7" | trial.3$ItemName == "R3P10" | trial.3$ItemName == "R3P13" | trial.3$ItemName == "R3P14")] <- 1

trial.3$MidCues[trial.3$Group == "DemPrimaryControl" & (trial.3$ItemName == "R3D10" | trial.3$ItemName == "R3D3" | trial.3$ItemName == "R3D5" |
                                           trial.3$ItemName == "R3D8" | trial.3$ItemName == "R3G1" | trial.3$ItemName == "R3G2" | trial.3$ItemName == "R3G5" |
                                           trial.3$ItemName == "R3G7" | trial.3$ItemName == "R3P10" | trial.3$ItemName == "R3P13" | trial.3$ItemName == "R3P14")] <- 1

###need to create date dummies: subjects who took the experiment early, mid, or late###
####broken down by the dates provided in the "event_creation_time" variable in the full dataset###
###early subjects: first third of takers###

x1<-rep.int(0, 30149)
trial.3$Early<-x1

trial.3$Early[trial.3$SubID <= 65125] <- 1

###late subjects: last third of takers###

x1<-rep.int(0, 30149)
trial.3$Late<-x1

trial.3$Late[trial.3$SubID >= 65450] <- 1




####first analysis: open probability by each experimental group####
###multilevel logit, clustering se's on subject and topic; controlling for the number of times the item appears###
####need to recode "ItemOpened" to be 1 or 0####

trial.3$Open<-recode(trial.3$ItemOpened, "0 = 0; else = 1")


####first analysis: open probability by treatment group. dummy variable for policy items###
####hypothesis 1: cues reduce open propensity####
####Model 1 and Model 2 in the paper####

trial.3$Policy<-recode(trial.3$Topic, "c('D1','D2','D3','D4','D5','D6','D7','D8','D9','D10','G1','G2','G3'
                       ,'G4','G5','G6','G7') = 0; else = 1")


si.hyp1 <-glmer(Open~ItemAppear + Policy + CommentOnly + LikesOnly + DislikesOnly + LikesCom + DislikesCom +  (1|SubID) + (1|Topic), data = trial.3, family = binomial)

si.hyp1z <-zelig (Open ~ ItemAppear + Policy + CommentOnly + LikesOnly + DislikesOnly + LikesCom + DislikesCom + tag(1|SubID) + tag(1|Topic), model = "logit.mixed", data = trial.3)

###okay, glmer and zeligmixed give same estimates###
###want zelig for 'sim' function; glmer for texreg###
###decrease in almost all cue groups, simplifying analysis down to treatment groups###
###getting predicted probabilities###
####comparing control group to cues group###
###based on the item appearing 2.5 times [mean]###

si.hyp1a <-zelig (Open ~ ItemAppear + Policy + SomeCues + tag(1|SubID) + tag(1|Topic), model = "logit.mixed", data = trial.3)
si.hyp1az <-glmer(Open~ItemAppear + Policy + SomeCues +  (1|SubID) + (1|Topic), data = trial.3, family = binomial)

x.base<-setx(si.hyp1az, ItemAppear = 2.5, SomeCues = 0)
x.treat<-setx(si.hyp1az, ItemAppear = 2.5,  SomeCues = 1)
hyp1 <-sim(si.hyp1az, x = x.base, x1 = x.treat)

x.base<-setx(si.hyp1az, ItemAppear = 2.5, Policy = 0, SomeCues = 0)
x.treat<-setx(si.hyp1az, ItemAppear = 2.5,  Policy = 0, SomeCues = 1)
hyp1a <-sim(si.hyp1az, x = x.base, x1 = x.treat)

x.base<-setx(si.hyp1az, ItemAppear = 2.5, Policy = 1, SomeCues = 0)
x.treat<-setx(si.hyp1az, ItemAppear = 2.5,  Policy = 1, SomeCues = 1)
hyp1b <-sim(si.hyp1az, x = x.base, x1 = x.treat)


####next, explore the mechanisms behind the decrease###
###Model 3 in the paper###
###Looking at effects for cued items, positive and negative groups, and for preferred candidate###
###hypothesis 2a: subjects more likely to view cued items, less likely to view non-cued items###

####combining high and mid-cued items into one group [NOTE: effect holds for both]###

x1<-rep.int(0, 30149)
trial.3$Cued <-x1

trial.3$Cued[trial.3$HiCues == 1 | trial.3$MidCues == 1] <- 1

###mixed-effects model###


si.hyp2az <-zelig (Open ~ ItemAppear + Cued + Preferred + CommentOnly +  PositiveGroup + NegativeGroup + I(PositiveGroup*Cued) + I(NegativeGroup*Cued) + I(PositiveGroup*Preferred) + I(NegativeGroup*Preferred) + tag(1|SubID) + tag(1|Topic), model = "logit.mixed", data = trial.3)
si.hyp2a <-glmer(Open~ItemAppear + Cued + Preferred+ CommentOnly + PositiveGroup + NegativeGroup + I(PositiveGroup*Cued) + I(NegativeGroup*Cued) + I(PositiveGroup*Preferred) + I(NegativeGroup*Preferred) + (1|SubID) + (1|Topic), data = trial.3, family = binomial)


###getting predicted probabilities####
###first, looking at the average open probability for cued items in control and treatment groups###


x.base<-setx(si.hyp2az, Cued = 1, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, Cued = 1, CommentOnly = 0)
hyp2a <-sim(si.hyp2az, x = x.base, x1 = x.treat)

x.base<-setx(si.hyp2az, Cued = 0, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, Cued = 0, CommentOnly = 0)
hyp2a1 <-sim(si.hyp2az, x = x.base, x1 = x.treat)

####now looking at differences by cue group--positive or negative####
####hypothesis 2b: subjects less likely to view positive cued items###
###predicted probabilities, positive cue groups###

x.base<-setx(si.hyp2az, Cued = 1, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, Cued = 1, CommentOnly = 0, Preferred = 0, PositiveGroup = 1, NegativeGroup = 0)
hyp2b1 <-sim(si.hyp2az, x = x.base, x1 = x.treat)

####predicted probabilities, negative cue groups###

x.base<-setx(si.hyp2az, Cued = 1, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, Cued = 1, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 1)
hyp2b2 <-sim(si.hyp2az, x = x.base, x1 = x.treat)

###so basically, positive cues did not increase views relative to control, but negative ones did decrease views###
###what about the non-cued items?###

x.base<-setx(si.hyp2az, Cued = 0, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, Cued = 0, CommentOnly = 0, Preferred = 0, PositiveGroup = 1, NegativeGroup = 0)
hyp2b3 <-sim(si.hyp2az, x = x.base, x1 = x.treat)

x.base<-setx(si.hyp2az, Cued = 0, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, Cued = 0, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 1)
hyp2b4 <-sim(si.hyp2az, x = x.base, x1 = x.treat)

###hypothesis 2c: subjects less likely to look at negative-cued items about preferred candidate###
###predicted probabilities###

###first establishing how subjects treat info about preferred candidate in the control group###

x.base<-setx(si.hyp2az, CommentOnly = 0, Preferred = 0, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, CommentOnly = 0, Preferred = 1, PositiveGroup = 0, NegativeGroup = 0)
hyp2c <-sim(si.hyp2az, x = x.base, x1 = x.treat)


####now looking at they react to cued information in the positive and negative groups###

x.base<-setx(si.hyp2az, CommentOnly = 0, Cued = 1, Preferred = 1, PositiveGroup = 0, NegativeGroup = 0)
x.treat<-setx(si.hyp2az, CommentOnly = 0, Cued = 1, Preferred = 1, PositiveGroup = 1, NegativeGroup = 0)
hyp2c1 <-sim(si.hyp2az, x = x.base, x1 = x.treat)

x.base<-setx(si.hyp2az, CommentOnly = 0, Cued = 0, Preferred = 1, PositiveGroup = 0, NegativeGroup = 1)
x.treat<-setx(si.hyp2az, CommentOnly = 0, Cued = 1, Preferred = 1, PositiveGroup = 0, NegativeGroup = 1)
hyp2c2 <-sim(si.hyp2az, x = x.base, x1 = x.treat)


###moving on to the evaluation portion of the analysis###
###want to see if cues affect subjects' propensity to like/dislike item###
####hypothesis 3: subjects more likely to 'like' items in positive condition, 'dislike' in negative###
###for this test, only subjects in the "social" version of the subject could like/dislike###
###subsetting data###


soc <- subset(trial.3, Group == "C1LR" | Group == "C3LD" | Group == "C1DR" | Group == "C3DD" |
                Group == "C1CDR" | Group == "C3CDD" | Group == "C1CLR" | Group == "C3CLD")

###furthermore, only opened items could be liked/disliked###


soc1 <- subset(soc, ItemOpened >= 1)

####probability of liking a cued item###
###Table 4, column 1 in the paper###

hyp.3 <-zelig (Liked ~ ItemOpened + Cued  + PositiveGroup  + I(PositiveGroup*Cued)  + tag(1|SubID) + tag(1|Topic), model = "logit.mixed", data = soc1)
hyp.3a <-glmer(Liked~ItemOpened + Cued + PositiveGroup  + I(PositiveGroup*Cued) + (1|SubID) + (1|Topic), data = soc1, family = binomial)


###predicted probabilities###

x.base<-setx(si.new5z, Cued = 1,  PositiveGroup = 0)
x.treat<-setx(si.new5z, Cued = 1, PositiveGroup = 1)
hyp.3.1 <-sim(si.new5z, x = x.base, x1 = x.treat)



###probability to disliking a cued item###
####Table 4, column 2 in the paper###

###dislikes####

hyp.3b <-zelig (Disliked ~ ItemOpened + Cued + PositiveGroup  + I(PositiveGroup*Cued) + tag(1|SubID) + tag(1|Topic), model = "logit.mixed", data = soc1)
hyp.3c <-glmer(Disliked~ItemOpened + Cued  + PositiveGroup  + I(PositiveGroup*Cued) + (1|SubID) + (1|Topic), data = soc1, family = binomial)


###predicted probabilities###

x.base<-setx(si.new5z1, Cued = 1, PositiveGroup = 0)
x.treat<-setx(si.new5z1, Cued = 1, PositiveGroup = 1)
hyp3b.1 <-sim(si.new5z1, x = x.base, x1 = x.treat)

####Next, want to look at the probability of voting for one of the manipulated candidates###
###Table 5 in the paper####
####dataset 'ft' has ratings and votes for the manipulated candidates####
###dataset was created by coding a vote as 1 if subject voted for the manipulated candidate in their group and 0 otherwise####
ft <-read.csv ("~/Documents/RFiles/ft.csv")

###for this analysis, need a count of the number of cued items examined to use as a covaritate###

temp<-subset(trial.3, Open == 1)
cuedopens <-aggregate(Cued ~ SubID, data = temp, FUN = sum)

###merging evaluation dataset with cuedopen dataset####

eval.final <-merge(ft,cuedopens, by = "SubID")
eval.final$Positive<-recode(eval.final$GROUP, "3 = 1; 7 = 1; 9 = 1; 11 = 1; else = 0")
eval.final$Negative<-recode(eval.final$GROUP, "4 = 1; 8 = 1; 10 = 1; 12 = 1; else = 0")
eval.final$CommentOnly<-recode(eval.final$GROUP, "5 = 1; 6 = 1; else = 0")

###checking dataset###
summary(eval.final)

###probability of voting for the manipulated candidate, by treatment group and number of cued items opened###

vt.out<-zelig(ManVote ~ Cued + CommentOnly + Negative + Positive + I(Cued * Negative) + I(Cued*Positive), model = "logit", data = eval.final)

###simulated probability over a range of Cued Item opens###
###for subjects in the positive group###

c.range <- seq(0,20, by = 2)

x.pos<-setx(vt.out, Cued = c.range, CommentOnly = 0, Negative = 0, Positive = 1)
pos.sim <-sim(vt.out, x = x.pos)

###for subjects in the negative group###

x.neg<-setx(vt.out, Cued = c.range, CommentOnly = 0, Negative = 1, Positive = 0)
neg.sim <-sim(vt.out, x = x.neg)


####creating the figure####
###extracting data from from the positive cued group###

ev <- data.frame(simulation.matrix(pos.sim,"Expected Values: Y|X"))

try.1<-melt(ev)

####getting the mean of the simulated data###
posdata <-aggregate(try.1, by=list(try.1$variable), 
                    FUN=mean, na.rm=TRUE)


###now I want the 2.5 and 97.5 percentiles###


X1<-quantile(ev$X1, c (.025,.975))
X2<-quantile(ev$X2, c (.025,.975))
X3<-quantile(ev$X3, c (.025,.975))
X4<-quantile(ev$X4, c (.025,.975))
X5<-quantile(ev$X5, c (.025,.975))
X6<-quantile(ev$X6, c (.025,.975))
X7<-quantile(ev$X7, c (.025,.975))
X8<-quantile(ev$X8, c (.025,.975))
X9<-quantile(ev$X9, c (.025,.975))
X10<-quantile(ev$X10, c (.025,.975))
X11<-quantile(ev$X11, c (.025,.975))

###putting it all into a data frame###
p.data <-rbind(X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11)

p.data<-as.data.frame(p.data)
colnames(p.data)<-c("Lower","Upper")


posdata$Lower<-p.data$Lower
posdata$Upper <-p.data$Upper

colnames(posdata)<-c("Obs","Variable","EValue","Lower","Upper")

###now want to do the same with negative group###

x.neg<-setx(vt.out, Cued = c.range, CommentOnly = 0, Negative = 1, Positive = 0)
neg.sim <-sim(vt.out, x = x.neg)

ev1 <- data.frame(simulation.matrix(neg.sim, "Expected Values: Y|X"))

try.2<-melt(ev1)

negdata <-aggregate(try.2, by=list(try.2$variable), 
                    FUN=mean, na.rm=TRUE)

X1<-quantile(ev1$X1, c (.025,.975))
X2<-quantile(ev1$X2, c (.025,.975))
X3<-quantile(ev1$X3, c (.025,.975))
X4<-quantile(ev1$X4, c (.025,.975))
X5<-quantile(ev1$X5, c (.025,.975))
X6<-quantile(ev1$X6, c (.025,.975))
X7<-quantile(ev1$X7, c (.025,.975))
X8<-quantile(ev1$X8, c (.025,.975))
X9<-quantile(ev1$X9, c (.025,.975))
X10<-quantile(ev1$X10, c (.025,.975))
X11<-quantile(ev1$X11, c (.025,.975))

n.data <-rbind(X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11)

n.data<-as.data.frame(n.data)
colnames(n.data)<-c("Lower","Upper")

negdata$Lower<-n.data$Lower
negdata$Upper <-n.data$Upper

colnames(negdata)<-c("Obs","Variable","EValue","Lower","Upper")

###putting positive and negative sim data into one dataset###
###creating dummy variables for treatment condition###
x1<-rep.int(0, 11)

posdata$Treatment <-x1

x1<-rep.int(1, 11)

negdata$Treatment <-x1

graph.data<-rbind(posdata,negdata)

###changing treatment into a factor for ease of plotting###

graph.data$Treatment<-factor(graph.data$Treatment, labels = c("Positive Cues","Negative Cues"))

###creating basic plot###

p <-ggplot(graph.data, aes(x = Obs, y = EValue, group = Treatment, linetype = Treatment)) + geom_ribbon(aes(ymin = Lower, ymax = Upper, fill = Treatment), alpha = .1) + geom_line(size = 1) 

###adding some labels###

p + scale_fill_manual(values = c("#D5DBDB", "#ABB2B9")) + theme_bw() + scale_x_discrete (labels = c("0", "2","4","6","8","10","12","14","16","18","20")) + xlab ("Cued Information Items Accessed") + ylab ("Predicted Vote Probability")

